package com.ow.module.event.caseHistoryMgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.ow.framework.common.Constant;
import com.ow.framework.data.po.ResultData;
import com.ow.framework.exception.DetailException;
import com.ow.framework.listener.event.EventExecutor;
import com.ow.framework.listener.event.EventListener;
import com.ow.module.event.sysmgr.UserCreateEvent;

public class SaveAnswerEvent implements EventListener {
	
	static Logger log = Logger.getLogger(Constant.FRAMEWORK);
	private ResultData resultData;
	public SaveAnswerEvent()
	{
		resultData = new ResultData();
	}

	 
	public ResultData event(EventExecutor eventExecutor) throws Exception {
		try
		{
			
			String sid = eventExecutor.getSqlidByIndex(0);
			HashMap paperMap = (HashMap)eventExecutor.getBaseAction().getParamMap();
			
			String caseHistoryID = (String)paperMap.get("caseHistoryID");
			String paperId = (String)paperMap.get("paper_id");
			Map assertTimesMap = new HashMap();
			assertTimesMap.put("aaAssertId", paperId);
			assertTimesMap.put("aaCaseHistoryId", caseHistoryID);
			Object obj = eventExecutor.getSqlSession().selectOne("assertAnswer.sel_paper_times_heaq", assertTimesMap);
			int assertTimes = 0;
			if (obj instanceof Integer){
				assertTimes = ((Integer)obj).intValue() + 1;
			}

			Map paperPerAnswer = new HashMap();
			paperPerAnswer.put("aaAssertId", paperId);
			paperPerAnswer.put("aaType", "paper");
			paperPerAnswer.put("aaCaseHistoryId", caseHistoryID);
			paperPerAnswer.put("aaAsertTimes", assertTimes);
			
			eventExecutor.getSqlSession().insert(sid, paperPerAnswer);
			
			Set set = paperMap.entrySet();
			Iterator it = set.iterator();
			String assertId = null;
			Map assertMap = new HashMap();
			while(it.hasNext()){
				Map.Entry<String, String> object = (Map.Entry<String, String>)it.next();
			    boolean isAssert = ((String)object.getKey()).startsWith("name_");
				if (isAssert) {
					assertId = ((String)object.getKey()).substring(5);
					assertMap.put("assertId", assertId);
					Map one = (HashMap)eventExecutor.getSqlSession().selectOne
					    ("assert.sel_assert_by_col", assertMap);
					Map answerMap = new HashMap();
					answerMap.put("aaCaseHistoryId", caseHistoryID);
					answerMap.put("aaType", one.get("atype"));
					answerMap.put("aaValue", ((String)object.getValue()));
					answerMap.put("aaAssertId", one.get("assertId"));
					answerMap.put("aaAssertCode", one.get("assertCode"));
					answerMap.put("aaAssertPid", one.get("pid"));
					answerMap.put("aaAssertPname", "");
					answerMap.put("aaValueType", "");
					answerMap.put("aaAsertTimes", assertTimes);
					
					if (!"".equals(sid))
					{
						int rvalue = eventExecutor.getSqlSession().insert(sid, answerMap);
						resultData.setIntResult(rvalue);
					}
				}
			}
			
			return resultData;
		}
		catch (Exception e)
		{
			String error = DetailException.expDetail(e, UserCreateEvent.class);
			log.error(error);
		}
		
		return resultData;
	}

}
